package com.mogu.user.service.impl;

import com.mogu.user.pojo.Email;
import com.mogu.user.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class EmailServiceImpl implements EmailService {

    @Value("${spring.mail.username}")
    private String sendFrom;

    @Resource
    JavaMailSender javaMailSender;

    @Override
    public String send(Email email) {
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(sendFrom);
            simpleMailMessage.setTo(email.getSendTo());
            simpleMailMessage.setSubject(email.getSubject());
            simpleMailMessage.setText(email.getMessage());
            javaMailSender.send(simpleMailMessage);
            return "success";
        } catch (MailException e) {
            System.out.println(e.getMessage());
        }
        return "fail";
    }
}
